import 'package:flutter/material.dart';
import '../theme/app_theme.dart';

/**
 * 设置页面
 * 提供应用配置和用户偏好设置
 */
class SettingsScreen extends StatefulWidget {
  const SettingsScreen({super.key});

  @override
  State<SettingsScreen> createState() => _SettingsScreenState();
}

class _SettingsScreenState extends State<SettingsScreen> {
  bool _darkMode = false;
  bool _autoSave = true;
  bool _notifications = true;
  String _fontSize = 'medium';
  String _language = 'zh_CN';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('设置'),
        backgroundColor: AppTheme.cardLight,
        elevation: 0,
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () => Navigator.pop(context),
        ),
      ),
      backgroundColor: AppTheme.surfaceLight,
      body: ListView(
        padding: const EdgeInsets.all(16),
        children: [
          // 外观设置
          _buildSectionCard(
            title: '外观',
            icon: Icons.palette_outlined,
            children: [
              _buildSwitchTile(
                title: '深色模式',
                subtitle: '使用深色主题',
                value: _darkMode,
                onChanged: (value) {
                  setState(() {
                    _darkMode = value;
                  });
                },
              ),
              _buildDropdownTile(
                title: '字体大小',
                subtitle: '调整文本显示大小',
                value: _fontSize,
                items: [
                  {'value': 'small', 'label': '小'},
                  {'value': 'medium', 'label': '中'},
                  {'value': 'large', 'label': '大'},
                ],
                onChanged: (value) {
                  setState(() {
                    _fontSize = value!;
                  });
                },
              ),
            ],
          ),
          
          SizedBox(height: 16),
          
          // 编辑器设置
          _buildSectionCard(
            title: '编辑器',
            icon: Icons.edit_outlined,
            children: [
              _buildSwitchTile(
                title: '自动保存',
                subtitle: '编辑时自动保存笔记',
                value: _autoSave,
                onChanged: (value) {
                  setState(() {
                    _autoSave = value;
                  });
                },
              ),
            ],
          ),
          
          SizedBox(height: 16),
          
          // 通知设置
          _buildSectionCard(
            title: '通知',
            icon: Icons.notifications_outlined,
            children: [
              _buildSwitchTile(
                title: '推送通知',
                subtitle: '接收应用通知',
                value: _notifications,
                onChanged: (value) {
                  setState(() {
                    _notifications = value;
                  });
                },
              ),
            ],
          ),
          
          SizedBox(height: 16),
          
          // 语言设置
          _buildSectionCard(
            title: '语言',
            icon: Icons.language_outlined,
            children: [
              _buildDropdownTile(
                title: '界面语言',
                subtitle: '选择应用界面语言',
                value: _language,
                items: [
                  {'value': 'zh_CN', 'label': '简体中文'},
                  {'value': 'en_US', 'label': 'English'},
                ],
                onChanged: (value) {
                  setState(() {
                    _language = value!;
                  });
                },
              ),
            ],
          ),
          
          SizedBox(height: 16),
          
          // 关于设置
          _buildSectionCard(
            title: '关于',
            icon: Icons.info_outline,
            children: [
              _buildActionTile(
                title: '版本信息',
                subtitle: 'LinkBook v1.0.0',
                onTap: () {
                  _showVersionDialog();
                },
              ),
              _buildActionTile(
                title: '用户协议',
                subtitle: '查看用户服务协议',
                onTap: () {
                  // TODO: 显示用户协议
                },
              ),
              _buildActionTile(
                title: '隐私政策',
                subtitle: '查看隐私保护政策',
                onTap: () {
                  // TODO: 显示隐私政策
                },
              ),
            ],
          ),
          
          SizedBox(height: 32),
          
          // 重置按钮
          Center(
            child: ElevatedButton.icon(
              onPressed: _showResetDialog,
              icon: Icon(Icons.refresh),
              label: Text('重置所有设置'),
              style: ElevatedButton.styleFrom(
                backgroundColor: Colors.red.shade50,
                foregroundColor: Colors.red.shade700,
                elevation: 0,
                padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
              ),
            ),
          ),
        ],
      ),
    );
  }

  /**
   * 构建设置分组卡片
   */
  Widget _buildSectionCard({
    required String title,
    required IconData icon,
    required List<Widget> children,
  }) {
    return Card(
      elevation: 2,
      shadowColor: Colors.black.withOpacity(0.1),
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // 分组标题
          Padding(
            padding: const EdgeInsets.all(16),
            child: Row(
              children: [
                Icon(
                  icon,
                  color: AppTheme.primaryColor,
                  size: 20,
                ),
                SizedBox(width: 8),
                Text(
                  title,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w600,
                    color: Colors.black87,
                  ),
                ),
              ],
            ),
          ),
          
          // 分组内容
          ...children,
        ],
      ),
    );
  }

  /**
   * 构建开关设置项
   */
  Widget _buildSwitchTile({
    required String title,
    required String subtitle,
    required bool value,
    required ValueChanged<bool> onChanged,
  }) {
    return ListTile(
      title: Text(
        title,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: Colors.black87,
        ),
      ),
      subtitle: Text(
        subtitle,
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
      trailing: Switch(
        value: value,
        onChanged: onChanged,
        activeColor: AppTheme.primaryColor,
      ),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }

  /**
   * 构建下拉选择设置项
   */
  Widget _buildDropdownTile({
    required String title,
    required String subtitle,
    required String value,
    required List<Map<String, String>> items,
    required ValueChanged<String?> onChanged,
  }) {
    return ListTile(
      title: Text(
        title,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: Colors.black87,
        ),
      ),
      subtitle: Text(
        subtitle,
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
      trailing: DropdownButton<String>(
        value: value,
        onChanged: onChanged,
        items: items.map((item) {
          return DropdownMenuItem<String>(
            value: item['value'],
            child: Text(
              item['label']!,
              style: TextStyle(fontSize: 14),
            ),
          );
        }).toList(),
        underline: SizedBox(),
      ),
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }

  /**
   * 构建操作设置项
   */
  Widget _buildActionTile({
    required String title,
    required String subtitle,
    required VoidCallback onTap,
  }) {
    return ListTile(
      title: Text(
        title,
        style: TextStyle(
          fontSize: 14,
          fontWeight: FontWeight.w500,
          color: Colors.black87,
        ),
      ),
      subtitle: Text(
        subtitle,
        style: TextStyle(
          fontSize: 12,
          color: Colors.black54,
        ),
      ),
      trailing: Icon(
        Icons.chevron_right,
        color: Colors.black54,
        size: 20,
      ),
      onTap: onTap,
      contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
    );
  }

  /**
   * 显示版本信息对话框
   */
  void _showVersionDialog() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('版本信息'),
        content: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text('LinkBook'),
            Text('版本: 1.0.0'),
            Text('构建: 2024.01.01'),
            SizedBox(height: 16),
            Text(
              '一款现代化的智能笔记管理应用',
              style: TextStyle(
                fontSize: 12,
                color: Colors.black54,
              ),
            ),
          ],
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('确定'),
          ),
        ],
      ),
    );
  }

  /**
   * 显示重置确认对话框
   */
  void _showResetDialog() {
    showDialog(
      context: context,
      builder: (context) => AlertDialog(
        title: Text('重置设置'),
        content: Text('确定要重置所有设置到默认值吗？此操作不可撤销。'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text('取消'),
          ),
          ElevatedButton(
            onPressed: () {
              // 重置所有设置
              setState(() {
                _darkMode = false;
                _autoSave = true;
                _notifications = true;
                _fontSize = 'medium';
                _language = 'zh_CN';
              });
              Navigator.pop(context);
              
              // 显示成功提示
              ScaffoldMessenger.of(context).showSnackBar(
                SnackBar(
                  content: Text('设置已重置'),
                  backgroundColor: Colors.green,
                ),
              );
            },
            style: ElevatedButton.styleFrom(
              backgroundColor: Colors.red,
              foregroundColor: Colors.white,
            ),
            child: Text('重置'),
          ),
        ],
      ),
    );
  }
}